package com.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Random;

/**
 * @author shinuojun by 2020/12/1 15:36
 */
public class RandomUtil {
    private static Random random = new Random();

    public RandomUtil() {
    }

    public static int random(int max) {
        return max < 2 ? 0 : random.nextInt(max);
    }

    public static int random(int min, int max) {
        int distance = max - min;
        return distance < 2 ? min : min + random.nextInt(distance + 1);
    }

    public static int randomLeftOut(int min, int max) {
        int distance = max - min;
        return distance <= 1 ? min : min + 1 + random.nextInt(distance);
    }

    public static int randomRightOut(int min, int max) {
        int distance = max - min;
        return distance <= 1 ? min : min + random.nextInt(distance);
    }

    public static List<Integer> randomMultiNumber(int min, int max, int count, boolean unique, Collection<Integer> excludeList) {
        ArrayList list = new ArrayList(count);

        while(true) {
            int value;
            do {
                do {
                    if (count <= 0) {
                        return list;
                    }

                    int distance = max - min;
                    value = min;
                    if (distance > 1) {
                        value = min + random.nextInt(distance + 1);
                    }
                } while(excludeList != null && excludeList.contains(value));
            } while(unique && list.contains(value));

            list.add(value);
            --count;
        }
    }

    public static List<Integer> randomMultiNumber(int min, int max, int count, boolean unique, int exclude) {
        ArrayList list = new ArrayList(count);

        while(true) {
            int value;
            do {
                do {
                    if (count <= 0) {
                        return list;
                    }

                    int distance = max - min;
                    value = min;
                    if (distance > 1) {
                        value = min + random.nextInt(distance + 1);
                    }
                } while(value == exclude);
            } while(unique && list.contains(value));

            list.add(value);
            --count;
        }
    }
}
